{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:50.461302Z",
     "start_time": "2024-12-21T14:08:49.902732Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n"
   ],
   "id": "1c6fba9b93745478",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:50.493896Z",
     "start_time": "2024-12-21T14:08:50.463299Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "df=pd.read_csv('天气.csv',encoding='utf-8')\n",
    "#data.columns = data.iloc[0]\n",
    "df.head()#前5列"
   ],
   "id": "b4da232d39f54cf0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         日期 最高温度  最低温度    天气   风力风向  空气指数\n",
       "0  2024/1/1  -1°  -12°  小雪~晴  西南风1级  48 优\n",
       "1  2024/1/2  -3°  -12°  晴~多云  东南风4级  63 良\n",
       "2  2024/1/3  -1°  -11°     晴  西北风2级  48 优\n",
       "3  2024/1/4   0°  -12°     晴   南风微风  54 良\n",
       "4  2024/1/5  -5°  -12°   阴~晴  东南风1级  78 良"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>最高温度</th>\n",
       "      <th>最低温度</th>\n",
       "      <th>天气</th>\n",
       "      <th>风力风向</th>\n",
       "      <th>空气指数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024/1/1</td>\n",
       "      <td>-1°</td>\n",
       "      <td>-12°</td>\n",
       "      <td>小雪~晴</td>\n",
       "      <td>西南风1级</td>\n",
       "      <td>48 优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024/1/2</td>\n",
       "      <td>-3°</td>\n",
       "      <td>-12°</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东南风4级</td>\n",
       "      <td>63 良</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024/1/3</td>\n",
       "      <td>-1°</td>\n",
       "      <td>-11°</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风2级</td>\n",
       "      <td>48 优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2024/1/4</td>\n",
       "      <td>0°</td>\n",
       "      <td>-12°</td>\n",
       "      <td>晴</td>\n",
       "      <td>南风微风</td>\n",
       "      <td>54 良</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2024/1/5</td>\n",
       "      <td>-5°</td>\n",
       "      <td>-12°</td>\n",
       "      <td>阴~晴</td>\n",
       "      <td>东南风1级</td>\n",
       "      <td>78 良</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:50.502337Z",
     "start_time": "2024-12-21T14:08:50.494892Z"
    }
   },
   "cell_type": "code",
   "source": "df[\"天气\"]",
   "id": "891111582b7c25a5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       小雪~晴\n",
       "1       晴~多云\n",
       "2          晴\n",
       "3          晴\n",
       "4        阴~晴\n",
       "       ...  \n",
       "350    中雪~小雪\n",
       "351      阴~晴\n",
       "352        晴\n",
       "353     多云~晴\n",
       "354        晴\n",
       "Name: 天气, Length: 355, dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:50.800814Z",
     "start_time": "2024-12-21T14:08:50.795282Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 处理天气，将“天气”列处理成只有晴、多云、雪\n",
    "def process_weather(weather):\n",
    "    if '晴' in weather:\n",
    "        return '晴'\n",
    "    elif '雪' in weather:\n",
    "        return '降水'\n",
    "    elif '雨' in weather:\n",
    "        return '降水'\n",
    "    else:\n",
    "        return '多云'\n",
    "\n",
    "# 应用处理函数到天气列\n",
    "df['天气'] = df['天气'].apply(process_weather)"
   ],
   "id": "3ab11cfb157bf5d5",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:51.370881Z",
     "start_time": "2024-12-21T14:08:51.347487Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 定义映射函数,0，1,2\n",
    "def map_weather(weather):\n",
    "    if '晴' in weather:\n",
    "        return 0\n",
    "    elif '多云' in weather:\n",
    "        return 1\n",
    "    elif '降水' in weather:\n",
    "        return 2\n",
    "    else:\n",
    "        return None  # 或者返回其他默认值\n",
    "\n",
    "# 应用映射函数到天气列\n",
    "df['编码'] = df['天气'].apply(map_weather)\n",
    "\n",
    "# 选择需要的列\n",
    "selected_columns = [ '天气','编码']\n",
    "df_selected = df[selected_columns]\n",
    "\n",
    "# 保存到新的CSV文件\n",
    "output_file_path = 'weather_process.csv'\n",
    "df_selected.to_csv(output_file_path, index=False, encoding='UTF-8')\n",
    "\n",
    "data=pd.read_csv('weather_process.csv',encoding='UTF-8')\n",
    "\n",
    "# 显示处理后的数据\n",
    "data.head()"
   ],
   "id": "7368f88407eaa8ad",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  天气  编码\n",
       "0  晴   0\n",
       "1  晴   0\n",
       "2  晴   0\n",
       "3  晴   0\n",
       "4  晴   0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>天气</th>\n",
       "      <th>编码</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>晴</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>晴</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>晴</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>晴</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>晴</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:51.944791Z",
     "start_time": "2024-12-21T14:08:51.897094Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#检查编码，excel里乱码，wps不乱码\n",
    "import chardet\n",
    "\n",
    "# 检测文件编码\n",
    "with open('weather_process.csv', 'rb') as f:\n",
    "    result = chardet.detect(f.read())\n",
    "\n",
    "print(result)"
   ],
   "id": "364972a82eed900a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:52.455256Z",
     "start_time": "2024-12-21T14:08:52.451523Z"
    }
   },
   "cell_type": "code",
   "source": [
    "states=data['编码'].tolist()\n",
    "print(states)"
   ],
   "id": "d69491b485fbca1a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 1, 2, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 1, 1, 2, 2, 0, 1, 0, 0, 0, 0, 2, 2, 1, 0, 2, 1, 0, 0, 0, 0, 2, 2, 1, 2, 1, 1, 0, 2, 2, 0, 0, 0, 0, 0, 0, 1, 2, 2, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 2, 0, 0, 0, 0, 1, 1, 2, 0, 0, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, 0, 0, 2, 1, 1, 0, 1, 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 2, 1, 2, 2, 1, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 2, 2, 0, 0, 1, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 2, 1, 1, 0, 0, 0, 0, 2, 2, 1, 0, 0, 2, 0, 1, 0, 1, 1, 2, 1, 0, 0, 2, 1, 0, 0, 0, 2, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 2, 2, 2, 1, 2, 0, 0, 2, 0, 0, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 1, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:53.190387Z",
     "start_time": "2024-12-21T14:08:53.183004Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#统计状态变化\n",
    "# 确定状态的范围\n",
    "num_states = 3  # 状态 0, 1, 2\n",
    "\n",
    "# 初始化状态转移矩阵\n",
    "transition_matrix = np.zeros((num_states, num_states), dtype=int)\n",
    "\n",
    "# 遍历状态序列，统计转移次数\n",
    "for i in range(len(states) - 1):\n",
    "    current_state = states[i]\n",
    "    next_state = states[i + 1]\n",
    "    transition_matrix[current_state, next_state] += 1\n",
    "\n",
    "# 打印转移矩阵\n",
    "print(\"状态转移矩阵 (转移次数):\")\n",
    "print(transition_matrix)\n",
    "\n",
    "# 归一化为转移概率（可选）\n",
    "transition_probabilities = transition_matrix / transition_matrix.sum(axis=1, keepdims=True)\n",
    "print(\"\\n状态转移概率矩阵:\")\n",
    "print(transition_probabilities)"
   ],
   "id": "e000999d7af0690a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "状态转移矩阵 (转移次数):\n",
      "[[142  39  21]\n",
      " [ 34  29  24]\n",
      " [ 26  19  20]]\n",
      "\n",
      "状态转移概率矩阵:\n",
      "[[0.7029703  0.19306931 0.1039604 ]\n",
      " [0.3908046  0.33333333 0.27586207]\n",
      " [0.4        0.29230769 0.30769231]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:08:58.309787Z",
     "start_time": "2024-12-21T14:08:53.906005Z"
    }
   },
   "cell_type": "code",
   "source": [
    "input_weather=input(\"今天天气晴：0\\n今天天气多云：1\\n今天天气降水(雨&雪)：2\\n\")\n",
    "if input_weather =='0':\n",
    "    input_weather=[1,0,0]\n",
    "elif input_weather =='1':\n",
    "    input_weather=[0,1,0]\n",
    "else:\n",
    "    input_weather=[0,0,1]\n",
    "input_weather=np.array(input_weather)\n",
    "\n",
    "matrix_power = []\n",
    "for power in range(1, 8):#计算p^2等\n",
    "    matrix_power.append(np.linalg.matrix_power(transition_probabilities, power))"
   ],
   "id": "2e2cef63a0cc83f2",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T14:09:00.841840Z",
     "start_time": "2024-12-21T14:09:00.826785Z"
    }
   },
   "cell_type": "code",
   "source": [
    "weather_val={0:\"晴天\",1:'多云',2:'降水天气'}\n",
    "for power in range(1,8):\n",
    "    predict_matrix = np.dot(input_weather, matrix_power[power-1])\n",
    "    max_index = np.argmax(predict_matrix)\n",
    "    print(f'第{power}天有{max(predict_matrix)}的概率是{weather_val[max_index]}')"
   ],
   "id": "8ab8f60fd34a9261",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1天有0.4的概率是晴天\n",
      "第2天有0.5185002319860633的概率是晴天\n",
      "第3天有0.554657024939748的概率是晴天\n",
      "第4天有0.5657300460453514的概率是晴天\n",
      "第5天有0.569122702045341的概率是晴天\n",
      "第6天有0.5701622337920039的概率是晴天\n",
      "第7天有0.5704807552244657的概率是晴天\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T11:48:56.393976Z",
     "start_time": "2024-12-21T11:48:56.392100Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "e8000ccdd46e832d",
   "outputs": [],
   "execution_count": 166
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-21T13:53:52.042929Z",
     "start_time": "2024-12-21T13:53:51.850480Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "transition_matri = np.zeros((3, 3), dtype=int)\n",
    "statu=[1,2,2,1,0,1]\n",
    "for i in range(len(statu) - 1):\n",
    "    current_statu = statu[i]\n",
    "    next_statu = statu[i + 1]\n",
    "    transition_matri[3,3] += 1\n",
    "\n",
    "# 打印转移矩阵\n",
    "print(\"状态转移矩阵 (转移次数):\")\n",
    "print(transition_matrix)"
   ],
   "id": "435edc956b831d30",
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "index 3 is out of bounds for axis 0 with size 3",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mIndexError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[3], line 7\u001B[0m\n\u001B[0;32m      5\u001B[0m     current_statu \u001B[38;5;241m=\u001B[39m statu[i]\n\u001B[0;32m      6\u001B[0m     next_statu \u001B[38;5;241m=\u001B[39m statu[i \u001B[38;5;241m+\u001B[39m \u001B[38;5;241m1\u001B[39m]\n\u001B[1;32m----> 7\u001B[0m     \u001B[43mtransition_matri\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m]\u001B[49m \u001B[38;5;241m+\u001B[39m\u001B[38;5;241m=\u001B[39m \u001B[38;5;241m1\u001B[39m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;66;03m# 打印转移矩阵\u001B[39;00m\n\u001B[0;32m     10\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m状态转移矩阵 (转移次数):\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n",
      "\u001B[1;31mIndexError\u001B[0m: index 3 is out of bounds for axis 0 with size 3"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "956f1dd9b24dee1c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
